package com.atguigu.gulimall.member.service.impl;

import com.atguigu.common.exception.PhoneException;
import com.atguigu.common.exception.UserNameException;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.vo.MemberUserLoginVo;
import com.atguigu.gulimall.member.vo.MemberUserRegisterVo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;

import javax.annotation.Resource;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Resource
    private MemberDao memberDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 注册***
     * @param memberUserRegisterVo
     * @return
     */
    @Override
    public void register(MemberUserRegisterVo memberUserRegisterVo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity  memberLevel=memberDao.selectMemberLevel();

        memberEntity.setLevelId(memberLevel.getId());
        checkPhoneUnique(memberUserRegisterVo.getPhone());
        checkUserNameUnique(memberUserRegisterVo.getUserName());
        memberEntity.setUsername(memberUserRegisterVo.getUserName());

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(memberUserRegisterVo.getPassword());
        memberEntity.setPassword(encode);

        memberEntity.setMobile(memberUserRegisterVo.getPhone());

    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(count>0){
            throw new PhoneException();
        }
    }

    @Override
    public void checkUserNameUnique(String username) throws UserNameException {
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if(count>0){
            throw new UserNameException();
        }
    }

    @Override
    public MemberEntity login(MemberUserLoginVo memberUserLoginVo) {
        String loginacct = memberUserLoginVo.getLoginacct();
        String password = memberUserLoginVo.getPassword();
        MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", loginacct).or()
                .eq("mobile", loginacct));
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(password, memberEntity.getPassword());
        if(matches){
            return memberEntity;
        }else {
            return null;
        }
    }

}